<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2017/3/13
 * Time: 10:13
 */

namespace Information\Controller;


use Common\Controller\AdminbaseController;
use Information\Model\LectureModel;

class LectureController extends AdminbaseController
{

    private $lecture_model;

    public function __construct()
    {
        parent::__construct();
        $this->lecture_model = new LectureModel();
    }

    public function lists(){

        if(IS_POST) {
            $keyword = I('post.title');
            $where['title'] = array('like', '%'.$keyword.'%');
        } else {
            $keyword = I('get.title');
            $where['title'] = array('like', '%'.$keyword.'%');
        }
        $count = $this->lecture_model
            ->where($where)
            ->count();
        $page = $this->page($count, C('PAGE_NUMBER'));
        $lectures = $this->lecture_model
            ->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order('id desc')
            ->select();
        foreach($lectures as $k => $v) {
            $result[$k]['str_manage'] .= '<a class="js-ajax-btn-dialog" href="' . U('Lecture/edit', ['id' => $v['id']]) . '">编辑</a> | <a class="js-ajax-delete" href="' . U('Lecture/delete', ['id' => $v['id']]) . '">删除</a>';
            if($v['hot'] == LectureModel::STATUS_HOT) {
                $hot = '热门 | <a class="js-ajax-btn-dialog" href="' . U('Lecture/editHot', ['id' => $v['id'], 'hot' => LectureModel::STATUS_NHOT]) . '">下热门</a>';
            } else {
                $hot = '非热门 | <a class="js-ajax-btn-dialog" href="' . U('Lecture/editHot', ['id' => $v['id'], 'hot' => LectureModel::STATUS_HOT]) . '">上热门</a>';
            }
			$strVipFlag=($v['VipFlag']>0)?"<span style='color:red;'>重要</span>":"普通";
            $lists  .= '<tr>
               <td>' . ($k + 1) . '</td>
               <td>' . $strVipFlag . '</td>
               <td>' . $v['title'].'</td>
               <td><img src="' . setUrl($v['cover']) . '?imageView2/2/h/30"></td>
               <td>' . $v['discribe'].'</td>
               <td>'.$v['create_time'] . '</td>
               <td>'.$hot . '</td>
               <td>' . $result[$k]['str_manage'] . '</td>
           </tr>';
        }

        $this->assign('lists', $lists);
        $this->assign('formget', I(''));
        $this->assign('Page',$page->show());
        $this->display();
    }

    public function add() {
        if(IS_POST) {
            $image = upload_img('Lecture');
            if(!$image) {
                $this->error('请添加封面');
            }
            $post = I('post.post');
            $data['discribe'] = $post['discribe'];
            $data['content'] = htmlspecialchars_decode($post['content']);
            $data['title'] = $post['title'];
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['cover'] = $image[0];
            $data['lec_link'] = $post['lec_link'];
			$data["VipFlag"]=$post["VipFlag"];
            if($this->lecture_model->add($data) == false) {
                $this->error();
            } else {
                $this->success();
            }
        } else {
            $this->display();
        }
    }

    public function edit() {
        $id = I('get.id');
        $info = $this->lecture_model->where(['id' => $id])->find();
        $info['cover'] = setUrl($info['cover']);
        if(IS_POST) {
            $id = I('post.id');
            $image = upload_img('Lecture');
            if($image) {
                $data['cover'] = $image[0];
            }
            $post = I('post.post');
            $data['discribe'] = $post['discribe'];
            $data['content'] = htmlspecialchars_decode($post['content']);
            $data['title'] = $post['title'];
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['lec_link'] = $post['lec_link'];
			$data["VipFlag"]=$post["VipFlag"];
            if($this->lecture_model->where(['id' => $id])->save($data) == false) {
                $this->error();
            } else {
                $this->success();
            }
        } else {
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function delete() {
        $id = I('get.id');
        if($this->lecture_model->where(['id' => $id])->delete() == false) {
            $this->error();
        } else {
            $this->success();
        }
    }

    /**
     * 上、下热门
     */
    public function editHot() {
        $id = I('get.id');
        $hot = I('get.hot');

        if($this->lecture_model->where(['id' => $id])->save(['hot' => $hot]) == false) {
            $this->error();
        } else {
            $this->success();
        }
    }
}